<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY Controller
*/
class MY_Controller extends CI_Controller{

	function __construct(){  
		parent::__construct();
		// 加载cache
        $this->load->driver('cache', array('adapter' => 'redis'));
	}

	// 对默认控制器无方法时的模板查询 for index
	public function _remap($method, $params= array()){
		if (method_exists($this, $method)){
			return call_user_func_array(array($this, $method), $params);
		}else{
			return call_user_func_array(array($this, "router"), $params);
		}
	}

	// 默认处理入口
	protected function router(){

		// 获取所有参数
		$uri = array_values($this->uri->segment_array());
		$uri_count = $this->uri->total_segments();

		// 参数容器
		$reg = array();
		// 数字的位置
		$num_index = 0;
		foreach ($uri as $i=> $v) {
			if (is_numeric($v)) {
				$num_index = $i;
				break;
			}
		}
		// 缝隙参数和真实地址
		if ($num_index >0) {
			$reg = array_slice($uri,$num_index,$uri_count);
			$uri = array_slice($uri,0,$num_index);
		}

		if ($uri_count) {
			// 假定文件
			$view_file = implode('/',$uri).EXT;
		}else{
			// 针对首页
			$view_file = "welcome.php";
			$uri[0] = '';
		}

		$vdata['uri'] = $uri;
		$vdata['reg'] = $reg;
		$vdata['url_base'] = implode('/',$uri);

		if (file_exists(VIEWS.$view_file)) {
			// CI, reg , header, base_url
			$this->load->view($view_file,$vdata);
		}else{
			dump($view_file,'[route][view_file]没有找到模版','error');
            show_404();
		}

	}

	// 验证码
	public function captchas_verify($str='')
	{
		$this->load->library('captcha');
		if($this->captchas_verify = $this->captcha->verify($str)){
			return true;
		}else{
			$this->form_validation->set_message('captchas_verify', '验证码输入错误');
			return false;
		}
	}

	// 验证码
	public function hashf_verify($str='')
	{
		if (!$this->session->flashdata('f_hashf')) {
			$this->form_validation->set_message('hashf_verify', '表单已经失效');
			return false;
		}

		if($str == $this->session->flashdata('f_hashf')){
			return true;
		}else{
			$this->form_validation->set_message('hashf_verify', '表单已经失效');
			return false;
		}
	}
        
    /**
     * 过滤数据
     */
    public function filterData($data,$isGet = ''){
        if(empty($data)){
            return '';
        }
        $method = $isGet ? 'get' : 'post';
        $returnData = array();
        foreach ($data as $k=>$v){                       
            $returnData[$k] = $this->input->post($k);       
        }
       return $returnData; 
    }  
}
